﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class RadiatorForm : HYBaseForm
	{
		public FamilySymbol Symbol
		{
			get
			{
				if (this.tabRadiator.SelectedIndex != 1)
				{
					return null;
				}
				return this.familySymbol;
			}
		}

		public RadiatorForm(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.InitControl();
			this.doc = doc;
			this.userSetting = new HVACUserSetting();
			this.InitializeRadiatorInOut();
			this.BingFamilysList(this.GetCurrPrjFamilys());
			this.ReadData();
		}

		public void AutoSelectRadiator()
		{
			if (this.spaceSelected != null)
			{
				Parameter parameter = this.spaceSelected.GetParameter(BuiltInParameter.ROOM_NAME);
				string text;
				if (parameter.AsString().Contains("洗") || parameter.AsString().Contains("卫"))
				{
					this.familyId = 8;
					text = "NDRTt-13040钢制散热器-DN15";
					this.raly.FamilyPath = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.doc), "Radiator", "NDRTt_13／040散热器_钢制_背篓_异侧同边接口.rfa");
				}
				else
				{
					this.familyId = 19;
					text = "铜铝复合帝雅Ⅱ系列-DN15";
					this.raly.FamilyPath = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.doc), "Radiator", "散热器_铜铝复合_竖排_异侧接口.rfa");
				}
				this.familyNameList.Clear();
				this.txtEquipmentName.Text = text;
				this.raly.FamilySymbol = this.txtEquipmentName.Text.Trim().Split(new char[]
				{
					'-'
				}).Last<string>();
				string familyNameById = this.sfdr.GetFamilyNameById(this.familyId, "Radiator");
				this.familyNameList.AddRange(familyNameById.Split(new char[]
				{
					';'
				}, StringSplitOptions.RemoveEmptyEntries));
			}
		}

		private void InitControl()
		{
			this.inGroup.Add(this.rbtnLTIn);
			this.inGroup.Add(this.rbtnRTIn);
			this.inGroup.Add(this.rbtnLBIn);
			this.inGroup.Add(this.rbtnRBIn);
			this.outGroup.Add(this.rbtnLTOut);
			this.outGroup.Add(this.rbtnRTOut);
			this.outGroup.Add(this.rbtnLBOut);
			this.outGroup.Add(this.rbtnRBOut);
		}

		private void btnSelectSpace_Click(object sender, EventArgs e)
		{
			RadiatorSelectSpaceForm radiatorSelectSpaceForm = new RadiatorSelectSpaceForm();
			radiatorSelectSpaceForm.ShowDialog();
			if (radiatorSelectSpaceForm.DialogResult == DialogResult.OK)
			{
				this.IsCurrentDoc = radiatorSelectSpaceForm.isCurrent;
				base.DialogResult = DialogResult.Retry;
			}
		}

		private void btnSelectEquipment_Click(object sender, EventArgs e)
		{
			SelectRadiatorForm selectRadiatorForm = new SelectRadiatorForm();
			if (selectRadiatorForm.ShowDialog() == DialogResult.OK)
			{
				this.familyNameList.Clear();
				this.familyId = selectRadiatorForm.familyId;
				this.familyNameList = selectRadiatorForm.familyName;
				StringBuilder radiatorName = selectRadiatorForm.radiatorName;
				this.txtEquipmentName.Text = radiatorName.ToString();
				this.raly.FamilySymbol = radiatorName.ToString().Split(new char[]
				{
					'-'
				}).Last<string>();
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (!this.CheckSelectedRadio())
			{
				YJKMessageBox.Information("请确认散热器进出水口的选择！");
				return;
			}
			if (this.spaceSelected == null)
			{
				YJKMessageBox.Information("请先选择空间");
				return;
			}
			if (!this.ReadUIData())
			{
				return;
			}
			this.raly.LayType = "0";
			base.DialogResult = DialogResult.OK;
		}

		private bool ReadUIData()
		{
			if (this.tabRadiator.SelectedIndex == 0)
			{
				if (this.txtEquipmentName.Text.Trim() == "")
				{
					YJKMessageBox.Information("请选择散热器");
					return false;
				}
			}
			else if (this.tabRadiator.SelectedIndex == 1 && this.tvwFamiysList.Nodes[0].Nodes.Count > 0 && this.familySymbol == null)
			{
				YJKMessageBox.Information("请选择散热器");
				return false;
			}
			if (!this.SaveData())
			{
				return false;
			}
			if (this.tabRadiator.SelectedIndex == 0 && this.familyNameList.Count > 0)
			{
				string radiatorInstallType = this.GetRadiatorInstallType();
				this.SetRadiatorFamilyPath(radiatorInstallType);
			}
			else
			{
				this.raly.FamilyPath = ((this.txtEquipmentName.Text.Trim() == "") ? "" : this.raly.FamilyPath);
			}
			return true;
		}

		private string GetRadiatorInstallType()
		{
			string text = "";
			foreach (RadioButton radioButton in this.inGroup)
			{
				if (radioButton.Checked)
				{
					text += radioButton.Tag.ToString();
					break;
				}
			}
			foreach (RadioButton radioButton2 in this.outGroup)
			{
				if (radioButton2.Checked)
				{
					text += radioButton2.Tag.ToString();
					break;
				}
			}
			return text;
		}

		private void SetRadiatorFamilyPath(string typeStr)
		{
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(typeStr);
			if (num <= 2279835011u)
			{
				if (num <= 518729469u)
				{
					if (num != 401286136u)
					{
						if (num != 501951850u)
						{
							if (num != 518729469u)
							{
								goto IL_34E;
							}
							if (!(typeStr == "13"))
							{
								goto IL_34E;
							}
							goto IL_239;
						}
						else
						{
							if (!(typeStr == "12"))
							{
								goto IL_34E;
							}
							goto IL_30B;
						}
					}
					else if (!(typeStr == "14"))
					{
						goto IL_34E;
					}
				}
				else if (num != 2247118416u)
				{
					if (num != 2263057392u)
					{
						if (num != 2279835011u)
						{
							goto IL_34E;
						}
						if (!(typeStr == "42"))
						{
							goto IL_34E;
						}
						goto IL_27F;
					}
					else
					{
						if (!(typeStr == "43"))
						{
							goto IL_34E;
						}
						goto IL_2C5;
					}
				}
				else
				{
					if (!(typeStr == "32"))
					{
						goto IL_34E;
					}
					goto IL_1F3;
				}
			}
			else if (num <= 2314375987u)
			{
				if (num != 2296612630u)
				{
					if (num != 2297451273u)
					{
						if (num != 2314375987u)
						{
							goto IL_34E;
						}
						if (!(typeStr == "24"))
						{
							goto IL_34E;
						}
						goto IL_27F;
					}
					else
					{
						if (!(typeStr == "31"))
						{
							goto IL_34E;
						}
						goto IL_239;
					}
				}
				else if (!(typeStr == "41"))
				{
					goto IL_34E;
				}
			}
			else if (num != 2347784130u)
			{
				if (num != 2364708844u)
				{
					if (num != 2398264082u)
					{
						goto IL_34E;
					}
					if (!(typeStr == "23"))
					{
						goto IL_34E;
					}
					goto IL_1F3;
				}
				else
				{
					if (!(typeStr == "21"))
					{
						goto IL_34E;
					}
					goto IL_30B;
				}
			}
			else
			{
				if (!(typeStr == "34"))
				{
					goto IL_34E;
				}
				goto IL_2C5;
			}
			this.raly.FamilyPath = this.familyNameList.Find((string n) => n.Contains("异侧") && !n.Contains("同边"));
			this.raly.RotateType = 0;
			goto IL_38F;
			IL_1F3:
			this.raly.FamilyPath = this.familyNameList.Find((string n) => n.Contains("异侧") && !n.Contains("同边"));
			this.raly.RotateType = 1;
			goto IL_38F;
			IL_239:
			this.raly.FamilyPath = this.familyNameList.Find((string n) => n.Contains("同侧"));
			this.raly.RotateType = 0;
			goto IL_38F;
			IL_27F:
			this.raly.FamilyPath = this.familyNameList.Find((string n) => n.Contains("同侧"));
			this.raly.RotateType = 1;
			goto IL_38F;
			IL_2C5:
			this.raly.FamilyPath = this.familyNameList.Find((string n) => n.Contains("异侧同边"));
			this.raly.RotateType = 0;
			goto IL_38F;
			IL_30B:
			this.raly.FamilyPath = this.familyNameList.Find((string n) => n.Contains("异侧同边"));
			this.raly.RotateType = 2;
			goto IL_38F;
			IL_34E:
			this.raly.FamilyPath = this.familyNameList.Find((string n) => n.Contains("异侧") && !n.Contains("同边"));
			this.raly.RotateType = 0;
			IL_38F:
			this.raly.FamilyPath = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.doc), "Radiator", this.raly.FamilyPath);
			this.raly.FamilySymbol = this.txtEquipmentName.Text.Trim().Split(new char[]
			{
				'-'
			}).Last<string>();
			this.raly.InstallType = typeStr;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void SetConnectorStatus(RadioButton rb)
		{
			if (rb.Name.ToString().Contains("In"))
			{
				using (List<RadioButton>.Enumerator enumerator = this.inGroup.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						RadioButton radioButton = enumerator.Current;
						if (rb.Name != radioButton.Name)
						{
							radioButton.Checked = false;
						}
					}
					return;
				}
			}
			foreach (RadioButton radioButton2 in this.outGroup)
			{
				if (rb.Name != radioButton2.Name)
				{
					radioButton2.Checked = false;
				}
			}
		}

		private void radiobtn_CheckedChanged(object sender, EventArgs e)
		{
			RadioButton radioButton = (RadioButton)sender;
			if (radioButton.Checked)
			{
				this.SetConnectorStatus(radioButton);
				if (!this.CheckSelectedRadio())
				{
					this.SetInOutRadioStatus(radioButton);
				}
			}
		}

		private void SetInOutRadioStatus(RadioButton rdb)
		{
			if (rdb.Name.Contains("In"))
			{
				this.RandomSelectRadio(this.outGroup, rdb.Tag.ToString());
				return;
			}
			this.RandomSelectRadio(this.inGroup, rdb.Tag.ToString());
		}

		private void RandomSelectRadio(List<RadioButton> rbGroup, string groupNum)
		{
			foreach (RadioButton radioButton in rbGroup)
			{
				if (radioButton.Tag.ToString() != groupNum)
				{
					radioButton.Checked = true;
					break;
				}
			}
		}

		private void RadiatorForm_Shown(object sender, EventArgs e)
		{
			this.InitlizeRaditor();
			this.AutoSelectRadiator();
		}

		private void btnAny_Click(object sender, EventArgs e)
		{
			if (!this.CheckSelectedRadio())
			{
				YJKMessageBox.Information("请确认散热器进出水口的选择！");
				return;
			}
			if (this.tvwFamiysList.Nodes[0].Nodes.Count == 0 && this.tabRadiator.SelectedIndex == 1)
			{
				return;
			}
			if (!this.ReadUIData())
			{
				return;
			}
			AnyLayTypeForm anyLayTypeForm = new AnyLayTypeForm();
			if (anyLayTypeForm.ShowDialog() == DialogResult.Cancel)
			{
				return;
			}
			this.IsSetNum = anyLayTypeForm.IsSetNum;
			this.Num = anyLayTypeForm.Num;
			this.IsDimension = anyLayTypeForm.IsDimension;
			this.raly.LayType = ((anyLayTypeForm.LayType == 0) ? "1" : "2");
			base.DialogResult = DialogResult.OK;
		}

		private bool CheckSelectedRadio()
		{
			bool result = false;
			int num = this.SelectedRadioNum(this.inGroup);
			int num2 = this.SelectedRadioNum(this.outGroup);
			if (num == 1 && num2 == 1)
			{
				result = true;
			}
			return result;
		}

		private int SelectedRadioNum(List<RadioButton> listRadio)
		{
			int num = 0;
			for (int i = 0; i < listRadio.Count; i++)
			{
				if (listRadio[i].Checked)
				{
					num++;
				}
			}
			return num;
		}

		private void ReadData()
		{
			if (this.userSetting.ReadValue("RadiatorParas", "Offset") != "")
			{
				this.hytbOffset.Text = this.userSetting.ReadValue("RadiatorParas", "Offset");
			}
			if (this.userSetting.ReadValue("RadiatorParas", "WallDistance") != "")
			{
				this.hytbWallDistance.Text = this.userSetting.ReadValue("RadiatorParas", "WallDistance");
			}
			if (this.tvwFamiysList.Nodes[0].Nodes.Count > 0)
			{
				this.hytxtCurrPrjRelativeLevel.Text = (string.IsNullOrEmpty(this.userSetting.ReadValue("RadiatorParas", "CurrPrjRelativeLevel")) ? "0.17" : this.userSetting.ReadValue("RadiatorParas", "CurrPrjRelativeLevel"));
			}
			if (this.tvwFamiysList.Nodes[0].Nodes.Count > 0)
			{
				this.hytxtCurrPrjOffWall.Text = (string.IsNullOrEmpty(this.userSetting.ReadValue("RadiatorParas", "CurrPrjOffWall")) ? "150" : this.userSetting.ReadValue("RadiatorParas", "CurrPrjOffWall"));
			}
			this.tabRadiator.SelectedIndex = ((this.userSetting.ReadValue("RadiatorParas", "YArchitechZu") != "") ? int.Parse(this.userSetting.ReadValue("RadiatorParas", "YArchitechZu")) : 0);
		}

		private bool SaveData()
		{
			if (this.tabRadiator.SelectedIndex == 0)
			{
				if (this.hytbOffset.Text.Trim() == "" || this.hytbWallDistance.Text.Trim() == "")
				{
					YJKMessageBox.Information("标高和距墙间距不能为空");
					return false;
				}
			}
			else if (this.tabRadiator.SelectedIndex == 1 && (this.hytxtCurrPrjRelativeLevel.Text.Trim() == "" || this.hytxtCurrPrjOffWall.Text.Trim() == ""))
			{
				YJKMessageBox.Information("标高和距墙间距不能为空");
				return false;
			}
			this.userSetting.WriteValue("RadiatorParas", "Offset", this.hytbOffset.Text);
			this.userSetting.WriteValue("RadiatorParas", "WallDistance", this.hytbWallDistance.Text);
			this.userSetting.WriteValue("RadiatorParas", "CurrPrjRelativeLevel", this.hytxtCurrPrjRelativeLevel.Text);
			this.userSetting.WriteValue("RadiatorParas", "CurrPrjOffWall", this.hytxtCurrPrjOffWall.Text);
			int num = (this.tabRadiator.SelectedIndex == 0) ? 0 : 1;
			this.userSetting.WriteValue("RadiatorParas", "YArchitechZu", num.ToString());
			if (this.tabRadiator.SelectedIndex == 0)
			{
				this.raly.Offset = double.Parse(this.hytbOffset.Text);
				this.raly.WallDistance = double.Parse(this.hytbWallDistance.Text);
			}
			if (this.tabRadiator.SelectedIndex == 1)
			{
				this.raly.Offset = double.Parse(this.hytxtCurrPrjRelativeLevel.Text);
				this.raly.WallDistance = double.Parse(this.hytxtCurrPrjOffWall.Text);
			}
			this.WriteRadiatorInOut();
			return true;
		}

		private List<Family> GetCurrPrjFamilys()
		{
			List<Family> list = new List<Family>();
			foreach (Element element in new FilteredElementCollector(this.doc).OfClass(typeof(Family)).ToElements())
			{
				Family family = (Family)element;
				if (family.FamilyCategory.Id.IntegerValue == -2001140 && family.Name.Contains("散热器"))
				{
					list.Add(family);
				}
			}
			return list;
		}

		private void BingFamilysList(List<Family> familys)
		{
			this.tvwFamiysList.Nodes.Clear();
			TreeNode treeNode = this.tvwFamiysList.Nodes.Add("机械设备");
			if (familys.Count == 0)
			{
				return;
			}
			foreach (Family family in familys)
			{
				TreeNode tnFamily = treeNode.Nodes.Add(family.Id.ToString(), family.Name);
				this.BingFamilySymbol(tnFamily, family);
			}
			this.tvwFamiysList.SelectedNode = this.tvwFamiysList.Nodes[0];
			this.tvwFamiysList.ExpandAll();
		}

		private void BingFamilySymbol(TreeNode tnFamily, Family family)
		{
			foreach (ElementId elementId in family.GetFamilySymbolIds())
			{
				FamilySymbol familySymbol = this.doc.GetElement(elementId) as FamilySymbol;
				tnFamily.Nodes.Add(familySymbol.Id.ToString(), familySymbol.Name);
			}
		}

		private void tvwFamiysList_AfterSelect(object sender, TreeViewEventArgs e)
		{
            this.tvwFamiysList.SelectedNode.BackColor = System.Drawing.Color.DodgerBlue;
            this.tvwFamiysList.SelectedNode.ForeColor = System.Drawing.Color.White;
			if (e.Node.Nodes.Count == 0 && e.Node.Parent != null)
			{
				ElementId elementId = new ElementId(int.Parse(e.Node.Name));
				this.familySymbol = (this.doc.GetElement(elementId) as FamilySymbol);
			}
			if (e.Node.Parent != null && e.Node.Nodes.Count != 0)
			{
				ElementId elementId2 = new ElementId(int.Parse(e.Node.Nodes[0].Name));
				this.familySymbol = (this.doc.GetElement(elementId2) as FamilySymbol);
			}
			this.tvwFamiysList.ExpandAll();
		}

		private void tvwFamiysList_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
			if (this.tvwFamiysList.SelectedNode != null)
			{
                this.tvwFamiysList.SelectedNode.BackColor = System.Drawing.Color.White;
                this.tvwFamiysList.SelectedNode.ForeColor = System.Drawing.Color.Black;
			}
		}

		private void InitializeRadiatorInOut()
		{
			string text = this.userSetting.ReadValue("RadiatorParas", "strIn");
			string text2 = this.userSetting.ReadValue("RadiatorParas", "strOut");
			if (!string.IsNullOrEmpty(text))
			{
				using (List<RadioButton>.Enumerator enumerator = this.inGroup.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						RadioButton radioButton = enumerator.Current;
						if (radioButton.Name == text)
						{
							radioButton.Checked = true;
						}
						else
						{
							radioButton.Checked = false;
						}
					}
					goto IL_8D;
				}
			}
			this.rbtnLTIn.Checked = true;
			IL_8D:
			if (!string.IsNullOrEmpty(text2))
			{
				using (List<RadioButton>.Enumerator enumerator = this.outGroup.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						RadioButton radioButton2 = enumerator.Current;
						if (radioButton2.Name == text2)
						{
							radioButton2.Checked = true;
						}
						else
						{
							radioButton2.Checked = false;
						}
					}
					goto IL_F2;
				}
			}
			this.rbtnRBOut.Checked = true;
			IL_F2:
			this.InitlizeRaditor();
		}

		private void InitlizeRaditor()
		{
			this.txtEquipmentName.Text = (string.IsNullOrEmpty(this.userSetting.ReadValue("RadiatorParas", "RadiatorType")) ? "钢制板式散热器-300型_DN15" : this.userSetting.ReadValue("RadiatorParas", "RadiatorType"));
			this.familyId = (string.IsNullOrEmpty(this.userSetting.ReadValue("RadiatorParas", "familyId")) ? 1 : int.Parse(this.userSetting.ReadValue("RadiatorParas", "familyId")));
			this.raly.FamilySymbol = this.txtEquipmentName.Text.Trim().Split(new char[]
			{
				'-'
			}).Last<string>();
			string familyNameById = this.sfdr.GetFamilyNameById(this.familyId, "Radiator");
			this.familyNameList.AddRange(familyNameById.Split(new char[]
			{
				';'
			}, StringSplitOptions.RemoveEmptyEntries));
		}

		private void WriteRadiatorInOut()
		{
			string value = "";
			string value2 = "";
			foreach (RadioButton radioButton in this.inGroup)
			{
				if (radioButton.Checked)
				{
					value = radioButton.Name;
					break;
				}
			}
			foreach (RadioButton radioButton2 in this.outGroup)
			{
				if (radioButton2.Checked)
				{
					value2 = radioButton2.Name;
					break;
				}
			}
			this.userSetting.WriteValue("RadiatorParas", "strIn", value);
			this.userSetting.WriteValue("RadiatorParas", "strOut", value2);
			this.userSetting.WriteValue("RadiatorParas", "RadiatorType", this.txtEquipmentName.Text);
			this.userSetting.WriteValue("RadiatorParas", "familyId", this.familyId.ToString());
		}

		private void RadiatorForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.WriteRadiatorInOut();
		}

		public List<Element> lisSpaceSelected = new List<Element>();

		public Element spaceSelected;

		private List<RadioButton> inGroup = new List<RadioButton>();

		private List<RadioButton> outGroup = new List<RadioButton>();

		private Document doc;

		private HVACUserSetting userSetting;

		public RadiatorAutoLayPara raly = new RadiatorAutoLayPara();

		private List<string> familyNameList = new List<string>();

		public bool IsCurrentDoc = true;

		public bool IsSetNum;

		public int Num;

		public bool IsDimension = true;

		private FamilySymbol familySymbol;

		private int familyId = 1;

		private SpaceFamilyDataReader sfdr = new SpaceFamilyDataReader();
	}
}
